﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Deployment.BuildManager.ExternalProcess.Common;

namespace Deployment.BuildManager.Common
{
    public class ExecuteTasks
    {
        public void Execute(string productName, string uriBranch, string uriSetup, string modulo, string version, string localPath, string packageTitle, List<string> descricoes)
        {
            Deployment.BuildManager.ExternalProcess.GetSource.BuildTask task1 = new Deployment.BuildManager.ExternalProcess.GetSource.BuildTask();
            Deployment.BuildManager.ExternalProcess.GetCompiledAssemblies.BuildTask task2 = new Deployment.BuildManager.ExternalProcess.GetCompiledAssemblies.BuildTask();
            Deployment.BuildManager.ExternalProcess.CreatePackage.BuildTask task3 = new Deployment.BuildManager.ExternalProcess.CreatePackage.BuildTask();
            Deployment.BuildManager.ExternalProcess.CreateXml.BuildTask task4 = new Deployment.BuildManager.ExternalProcess.CreateXml.BuildTask();
            Deployment.BuildManager.ExternalProcess.Compiler.BuildTask task5 = new Deployment.BuildManager.ExternalProcess.Compiler.BuildTask();
            Deployment.BuildManager.ExternalProcess.CheckInAssemblies.BuildTask task6 = new Deployment.BuildManager.ExternalProcess.CheckInAssemblies.BuildTask();

            task1.MessageReceived += new EventHandler<Deployment.BuildManager.ExternalProcess.Common.MessageEventArgs>(task_MessageReceived);
            task2.MessageReceived += new EventHandler<Deployment.BuildManager.ExternalProcess.Common.MessageEventArgs>(task_MessageReceived);
            task3.MessageReceived += new EventHandler<Deployment.BuildManager.ExternalProcess.Common.MessageEventArgs>(task_MessageReceived);
            task4.MessageReceived += new EventHandler<Deployment.BuildManager.ExternalProcess.Common.MessageEventArgs>(task_MessageReceived);
            task5.MessageReceived += new EventHandler<Deployment.BuildManager.ExternalProcess.Common.MessageEventArgs>(task_MessageReceived);
            task6.MessageReceived += new EventHandler<Deployment.BuildManager.ExternalProcess.Common.MessageEventArgs>(task_MessageReceived);

            Deployment.BuildManager.ExternalProcess.Common.SetupInfo info = new Deployment.BuildManager.ExternalProcess.Common.SetupInfo();

            info.CompiledUri = uriBranch;
            info.TemplateProjectUri = "$/Deployment/NET/DEV/CDSC/VN/Source/BuildManager/ExternalProcess/CreateSetupFile";
            info.SetupUri = uriSetup;
            info.DownloadPath = localPath;
            info.ProductName = productName;
            info.ModuleName = modulo;
            info.BuildDescriptions = descricoes;
            info.Version = new PSE.Framework.Common.Version(version);
            info.PackageTitle = packageTitle;

            //Logger.Log("Inicio do processo de empacotamento.", Logger.EntryType.Information);
            //Logger.Log(LogSetupInfoInformation(info), Logger.EntryType.Information);

            this.SendMessage("Início do Processo de Empacotamento.");
            this.SendMessage(LogSetupInfoInformation(info));

            try
            {
                //Download source.
                task1.Execute(info);

                //Copia arquivos para pasta de resource.
                task2.Execute(info);

                //Cria o projeto de setup.
                task3.Execute(info);

                //Cria SetupConfiguration.xml
                task4.Execute(info);

                //Compila projeto de setup
                task5.Execute(info);

                //Faz check-in
                //task6.Execute(info);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.SendMessage("Fim do Processo de Empacotamento.");
            }
        }

        private string LogSetupInfoInformation(SetupInfo info)
        {
            string message = String.Empty;
            message += System.Environment.NewLine;
            message += "Setup Template Project: " + info.TemplateProjectUri + System.Environment.NewLine;
            message += "Uri Setup: " + info.SetupUri + System.Environment.NewLine;
            message += "Download Path: " + info.DownloadPath + System.Environment.NewLine;
            message += "Product Name: " + info.ProductName + System.Environment.NewLine;
            message += "Module Name: " + info.ModuleName + System.Environment.NewLine;
            return message;
        }

        private void SendMessage(string message)
        {
            if (MessageReceived != null)
            {
                MessageEventArgs eventArgs = new MessageEventArgs(message);
                Logger.Log(message, Logger.EntryType.Information);
                MessageReceived(this, eventArgs);
            }
        }

        #region [ Events ]

        public event EventHandler<MessageEventArgs> MessageReceived;

        void task_MessageReceived(object sender, MessageEventArgs e)
        {
            if (e != null)
            {
                MessageEventArgs eventArgs = new MessageEventArgs(e.Message);
                MessageReceived(this, eventArgs);
            }
        }

        #endregion [ Events ]
    }
}
